﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TianYu.CMS.Domain.DomainModel;
using TianYu.CMS.Domain.ViewModel.Request;
using TianYu.CMS.Infrastructure.Enums;
using TianYu.CMS.Service;
using TianYu.Core.Common;
using TianYu.Core.Common.BaseViewModel;

namespace TianYu.CMS.WebMvc.Areas.Admin.Controllers
{
    public class SysRoleController : BaseController
    {
        private ISysRoleService _sysRoleService;
        private ISysMenuService _sysMenuService;
        private ISysRoleMenuService _sysRoleMenuService;

        public SysRoleController(ISysRoleService sysRoleService, ISysMenuService sysMenuService, ISysRoleMenuService sysRoleMenuService)
        {
            this._sysRoleService = sysRoleService;
            this._sysMenuService = sysMenuService;
            this._sysRoleMenuService = sysRoleMenuService;
        }




        public async Task<IActionResult> Index()
        {
            var menuList = await _sysMenuService.FindUserMenu(base.LoginUserGuid);// _sysMenuService.Query(o => o.Status == StatusEnum.Effective, o => o.Sort, OrderByType.Asc);

            return View(menuList);
        }
        public async Task<IActionResult> AddEdit(int? id)
        {
            var model = new SysRole();

            if (id.HasValue)
            {
                model = await _sysRoleService.GetAsync(id);
            }
            return View(model);
        }



        public async Task<AjaxPageResult<List<SysRole>>> GetList([FromQuery] SysRoleListRequestModel requestModel)
        {
            var res = await _sysRoleService.QueryPageAsync(o => o.Status == StatusEnum.Effective, requestModel.PageIndex, requestModel.PageSize);

            return res.list.ToViewModel(requestModel.PageIndex, res.total);
        }
        public async Task<AjaxResult<bool>> Save(SaveSysRoleRequestModel requestModel)
        {
            if (requestModel.RoleName.IsNullOrWhiteSpace())
            {
                return AjaxResult<bool>.Fail("用户角色不能为空");
            }

            var model = requestModel.MapTo<SysRole>();

            bool result = false;

            if (requestModel.Id == 0)
            {
                model.Enabled = true;
                model.CreateGuid = base.LoginUserGuid;
                model.CreateTime = DateTime.Now;
                model.Creator = base.LoginUserName;

                result = await _sysRoleService.InsertAsync(model) > 0;
            }
            else
            {
                result = await _sysRoleService.UpdateAsync(o => new SysRole()
                {
                    RoleName = model.RoleName,
                    Enabled = model.Enabled,
                    Modifier = LoginUserName,
                    ModifyGuid = LoginUserGuid,
                    ModifyTime = DateTime.Now
                }, x => x.Id == model.Id);
            }

            return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
        }
        public async Task<AjaxResult<bool>> Remove(int id)
        {
            bool result = await _sysRoleService.UpdateAsync(o => new SysRole() { Status = Infrastructure.Enums.StatusEnum.Invalid }, o => o.Id == id);

            return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("重置失败");
        }
        public async Task<AjaxResult<List<SysRoleMenu>>> GetRolePower(int id)
        {
            var list = await _sysRoleMenuService.QueryAsync(x => x.RoleId == id);

            return list.ToViewModel();
        }
        public async Task<AjaxResult<bool>> SaveRolePower(int id, int[] menuIds)
        {
            _sysRoleMenuService.DeleteQueue(a => a.RoleId == id);

            if (menuIds.Length > 0)
            {
                List<SysRoleMenu> list = new List<SysRoleMenu>();

                foreach (var item in menuIds)
                {
                    list.Add(new SysRoleMenu()
                    {
                        MenuId = item,
                        RoleId = id
                    });
                }

                _sysRoleMenuService.InsertQueue(list);
            }
            var result = await _sysRoleMenuService.SaveQueuesAsync();

            return result > 0 ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
        }
    }
}